<?php

namespace App\Models\Stock;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class StockAllotOrder extends Model
{
    protected $table = 'stock_allot_order';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request, $cpid)
    {
        $query = self::with(['suborder'])->where(['cpid' => $cpid])->orderBy('create_at','desc');

        $start_time = strtotime($request->start_time)>0?strtotime($request->start_time):intval($request->start_time);
        $end_time = strtotime($request->end_time)>0?strtotime($request->end_time):intval($request->end_time);
        if ($start_time > 0 && $end_time > 0) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        //$order_sn_code = $request->order_sn_code; //采购订单单据编号
        $order_sn_code = $request->voucher_code;
        if ($order_sn_code != '') {
            $query->where('order_sn_code', 'like', '%' . $order_sn_code . '%');
        }

        $warehouse_id_out=$request->warehouse_id_out;
        if($warehouse_id_out>0){
            $query->where('warehouse_id_out',$warehouse_id_out);
        }

        $warehouse_id_in=$request->warehouse_id_in;
        if($warehouse_id_in>0){
            $query->where('warehouse_id_in',$warehouse_id_in);
        }

        $isgetall=$request->isgetall ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }

    }


    public function suborder()
    {
        return $this->hasMany(StockAllotOrderSub::class, 'order_id', 'id');
    }
}
